package com.zenith.zongheng.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zenith.zongheng.core.exception.ProcessException;
import com.zenith.zongheng.demo.constant.LeaveResultConstant;
import com.zenith.zongheng.demo.domain.dto.LeaveStatisticsDTO;
import com.zenith.zongheng.demo.domain.entity.LeaveInfo;
import com.zenith.zongheng.demo.dao.LeaveInfoMapper;
import com.zenith.zongheng.demo.api.LeaveInfoService;
import com.zenith.zongheng.demo.domain.dto.LeaveInfoDTO;
import com.zenith.zongheng.demo.domain.dto.LeaveInfoSaveDTO;
import com.zenith.zongheng.demo.domain.vo.LeaveInfoVO;
import com.zenith.zongheng.demo.converter.LeaveInfoConverter;
import com.zenith.zongheng.common.domain.vo.PageVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.zongheng.system.api.SysUnitinfoService;
import com.zenith.zongheng.system.api.SysUnituserService;
import com.zenith.zongheng.system.api.SysUserinfoService;
import com.zenith.zongheng.system.domain.entity.SysUserinfo;
import com.zenith.zongheng.system.domain.vo.SysUnitinfoVO;
import com.zenith.zongheng.system.domain.vo.SysUserinfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tangzx
 * @since 2021-12-07
 */

@Service
@org.apache.dubbo.config.annotation.Service
public class LeaveInfoServiceImpl extends ServiceImpl<LeaveInfoMapper, LeaveInfo> implements LeaveInfoService {

    @Autowired
    private LeaveInfoConverter leaveInfoConverter;

    @Autowired
    SysUnitinfoService sysUnitinfoService;

    @Autowired
    SysUserinfoService sysUserinfoService;

    @Autowired
    SysUnituserService sysUnituserService;

    @Override
    public PageVO<LeaveInfoVO> queryPage(LeaveInfoDTO dto) {
        IPage<LeaveInfoVO> page = baseMapper.selectPage(new Page<>(dto.getCurrPage(), dto.getPageSize()), dto);
        return new PageVO<>(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
    }

    @Override
    @Transactional(rollbackFor = ProcessException.class)
    public void saveLeaveInfo(LeaveInfoSaveDTO dto) {
        LeaveInfo entity = leaveInfoConverter.dto2LeaveInfo(dto);
        baseMapper.insert(entity);
    }


    @Override
    @Transactional(rollbackFor = ProcessException.class)
    public void updateLeaveInfo(LeaveInfoSaveDTO dto) {
        LeaveInfo entity = leaveInfoConverter.dto2LeaveInfo(dto);
        baseMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = ProcessException.class)
    public void deleteLeaveInfo(Long... ids) {
        this.removeByIds(Arrays.asList(ids));
    }

    @Override
    @Transactional(rollbackFor = ProcessException.class)
    public Integer leaveCountByYear(Long unitUserId) {
        if (unitUserId == null) {
            throw new ProcessException(LeaveResultConstant.UNIT_USER_ID_NULL);
        }
        return baseMapper.countLeaveByYear(unitUserId);
    }

    /**
     * <p>
     * 统计实现类
     * 传入统计开始时间、统计结束时间、统计部门id将会返回该部门所有信息的map集合
     * </p>
     *
     * @author zza
     * @since 2021-02-07
     */
    @Override
    @Transactional(rollbackFor = ProcessException.class)
    public Map<SysUnitinfoVO,List<Map<SysUserinfo, List<LeaveInfo>>>> statistics(LeaveStatisticsDTO dto) {
        Long topId=dto.getUnitId();
        List<SysUnitinfoVO> childUnit = sysUnitinfoService.getChildUnit(topId);
        Map<SysUnitinfoVO, List<Map<SysUserinfo, List<LeaveInfo>>>> unitUserMap = new HashMap<>();
        for (int i = 0; i < childUnit.size(); i++) {
            List<SysUserinfoVO> userOrSubUserListByUnitId = sysUnituserService.getUserOrSubUserListByUnitId(childUnit.get(i).getId(), false);
            List<Map<SysUserinfo,List<LeaveInfo>>> mapList= new ArrayList<>();
            for (int j = 0; j < userOrSubUserListByUnitId.size(); j++) {
                QueryWrapper<LeaveInfo> leaveInfoQueryWrapper = new QueryWrapper<>();
                leaveInfoQueryWrapper.eq("unit_user_id", userOrSubUserListByUnitId.get(j).getId());
                leaveInfoQueryWrapper.ge("start_time",dto.getStartDate());
                leaveInfoQueryWrapper.le("start_time",dto.getEndDate());
                List<LeaveInfo> leaveInfos = baseMapper.selectList(leaveInfoQueryWrapper);
                SysUserinfo sysUserinfo = sysUserinfoService.getById(userOrSubUserListByUnitId.get(j).getId());
                Map<SysUserinfo,List<LeaveInfo>> userLeaveInfoMap = new HashMap<>();
                userLeaveInfoMap.put(sysUserinfo,leaveInfos);
                mapList.add(userLeaveInfoMap);
            }
            unitUserMap.put(childUnit.get(i),mapList);
        }
        return unitUserMap;
    }

}
